package cn.xaut.剑指offer.m20;

public class TreeNode {
    
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
        
    }
    
    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    // 前序遍历
    public void preorder(TreeNode node) {
        
        if (node == null)
            return;

        System.out.println(node.val);
        preorder(node.left);
        preorder(node.right);
    }

    // 中序遍历
    public void inorder(TreeNode node) {
        
        if (node == null)
            return;

        inorder(node.left);
        System.out.println(node.val);
        inorder(node.right);
    }

    // 查看该树是否包含某一节点
    public boolean contain(TreeNode node, int key) {
        
        // 递归终止条件
        if (node == null)
            return false;
        
        if (node.val == key)
            return true;

        // 递归过程
        return (contain(node.left, key) || contain(node.right, key));
    }
}
